$(document).ready(function(){   

    var num_user = $(document).find('#num_user').text();

    var cookieName = 'accordionUser'+num_user;

    var accordion = $( "#accordion" ).accordion({
        collapsible: true,
        autoHeight: false,
        active: ( $.cookies.get( cookieName ) || 0 ),
        change: function( e, ui )
        {
            $.cookies.set( cookieName, $( this ).find( 'h3' ).index ( ui.newHeader[0] ) );
        }
    });
    
    
    var listLot = $('#listLot').dataTable( {
        "bAutoWidth": false,
        "bJQueryUI": true,
        "bPaginate": true,
	"bSort": true,
        "bStateSave": false,
        "sPaginationType": "full_numbers",
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": "/user/listLot",
        "oLanguage":
        {
            "sUrl": "/datatable/language"
        },
        "aoColumnDefs": [ {"bSortable": false, "aTargets": [ 7, 9 ]} ],
        "fnServerParams": function ( aoData ) {
            aoData.push( {"name": "idUser", "value": num_user} );
        },
        "fnInitComplete": function(oSettings, json) {
            $('#listLot_filter').find(":text").qtip({
                content: $('#listLot_qtip'),
                position: {
                        my: 'bottom center', 
                        at: 'top center'
                },
                show: {
                    event: 'click',
                    effect: function() {
                        $(this).stop(0, 1).fadeIn(400);
                    }
                },
                hide: {
                    event: 'unfocus',
                    effect: function() {
                        $(this).stop(0, 1).fadeOut(400);
                    }
                },
                style: { 
                        tip: true,
                        classes: 'ui-tooltip-blue ui-tooltip-shadow ui-tooltip-rounded'
                }
            });  
        }
   });
   
   
   
   $('.modifDroit').live('click',function(e)
    {
        e.preventDefault();
        var idLot = $(this).attr('id').split('_')[1];
        var idTypeDroit = $(this).attr('id').split('_')[2];
        modifDroit(num_user, idLot, idTypeDroit, listLot);
    });
    
    $('.deleteDroit').live('click',function(e)
    {
        e.preventDefault();
        var idLot = $(this).attr('id').split('_')[1];
        deleteDroit(num_user, idLot, listLot);
    });
    
    

    
    $("#admin_cb-enable").click(function(){
       if(!$(this).hasClass("selected")) promouvoirUser(num_user, $(this));
    });
    
    $("#admin_cb-disable").click(function(){
        if(!$(this).hasClass("selected")) degraderUser(num_user, $(this));
    });
        
    $("#actif_cb-enable").click(function(){
       if(!$(this).hasClass("selected")) activerUser(num_user, $(this));
    });
    
    $("#actif_cb-disable").click(function(){
        if(!$(this).hasClass("selected")) desactiverUser(num_user, $(this));
    });
    
});

function modifDroit(idUser, idLot, idTypeDroit, listLot){
    return $.ajax({
        type:'GET',
        url : "/droitsUtilisateur/modifDroit",
        data : {
            id_user:idUser,
            id_lot:idLot,
            id_type_droit:idTypeDroit
        },
        success: function(){
            listLot.fnDraw(false);
        }
    });
}

function deleteDroit(idUser, idLot, listLot){
    return $.ajax({
        type:'GET',
        url : "/droitsUtilisateur/deleteDroit",
        data : {
            id_user:idUser,
            id_lot:idLot
        },
        success: function(){
            listLot.fnDraw(false);
        }
    });
}

function promouvoirUser(id, bouton){
    var prenom_user = $(document).find('#prenom_user').text();
    var nom_user = $(document).find('#nom_user').text();
    var noty_help = null;
    return $.ajax({
        type:'GET',
        url : "/user/promouvoirUser",
        data : {id:id},
        dataType: 'json',
        success: function(data){
            if(data.status == "success") switchBoutonOnOff(bouton, 1);
            noty_help = myNoty(data.msg, data.status);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            noty_help = myNoty("Erreur : "+errorThrown, "error");
        }
    });
}

function degraderUser(id, bouton){
    var prenom_user = $(document).find('#prenom_user').text();
    var nom_user = $(document).find('#nom_user').text();
    var noty_help = null;
    return $.ajax({
        type:'GET',
        url : "/user/degraderUser",
        data : {id:id},
        dataType: 'json',
        success: function(data){
            if(data.status == "success") switchBoutonOnOff(bouton, 0);
            noty_help = myNoty(data.msg, data.status);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            noty_help = myNoty("Erreur : "+errorThrown, "error");
        }
    });
}

function activerUser(id, bouton){
    var prenom_user = $(document).find('#prenom_user').text();
    var nom_user = $(document).find('#nom_user').text();
    var noty_help = null;
    return $.ajax({
        type:'GET',
        url : "/user/activerUser",
        data : {id:id},
        dataType: 'json',
        success: function(data){
            if(data.status == "success") switchBoutonOnOff(bouton, 1);
            noty_help = myNoty(data.msg, data.status);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            noty_help = myNoty("Erreur : "+errorThrown, "error");
        }
    });
}

function desactiverUser(id, bouton){
    var prenom_user = $(document).find('#prenom_user').text();
    var nom_user = $(document).find('#nom_user').text();
    var noty_help = null;
    return $.ajax({
        type:'GET',
        url : "/user/desactiverUser",
        data : {id:id},
        dataType: 'json',
        success: function(data){
            if(data.status == "success") switchBoutonOnOff(bouton, 0);
            noty_help = myNoty(data.msg, data.status);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            noty_help = myNoty("Erreur : "+errorThrown, "error");
        }
    });
}